﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using agsXMPP;

namespace MyClient
{
    public partial class LoginForm : Form
    {
        //public XmppClientConnection Conn { get; set; }

        public XmppClientConnection xmppConn;
        
        public LoginForm(XmppClientConnection _conn)
        {
            InitializeComponent();
            xmppConn = _conn;

            tb_name.Text = "yobdc";
            tb_passwd.Text = "qqqqq";
            tb_server.Text = "localhost";
        }

        private void bt_login_Click(object sender, EventArgs e)
        {
            string str_jid = tb_name.Text.Trim() +"@" + tb_server.Text.Trim();
            Jid jid = new Jid(str_jid);

            //jid = new Jid(tb_name.Text, tb_server.Text, "ooxx");
            System.Net.IPHostEntry iphe = System.Net.Dns.Resolve(tb_server.Text);
            for (int i = 0; i < iphe.AddressList.Length; i++)
            {
                Console.WriteLine("IP address: {0}", iphe.AddressList[i].ToString());
                //bsIq.Query.AddStreamHost(m_XmppCon.MyJID, iphe.AddressList[i].ToString(), 1000);
            } 

            xmppConn.Username = jid.User;
            xmppConn.Server = jid.Server;
            xmppConn.Password = tb_passwd.Text.Trim();

            xmppConn.AutoAgents = false;
            xmppConn.AutoPresence = true;
            xmppConn.AutoRoster = true;
            xmppConn.AutoResolveConnectServer = true;

            xmppConn.OnLogin += new ObjectHandler(Conn_OnLogin);
            xmppConn.OnError += new ErrorHandler(Conn_OnError);
            xmppConn.OnSocketError += new ErrorHandler(Conn_OnSocketError);
            xmppConn.OnReadXml += new XmlHandler(xmppConn_OnReadXml);
            xmppConn.OnWriteXml +=new XmlHandler(xmppConn_OnWriteXml);

            xmppConn.OnReadSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(xmppConn_OnReadSocketData);
            xmppConn.OnWriteSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(xmppConn_OnWriteSocketData);

            System.Net.IPAddress[] addr = System.Net.Dns.GetHostAddresses(xmppConn.Server);

            try
            {
                xmppConn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Wait("Login to server, please wait");
        }

        void xmppConn_OnWriteXml(object sender, string xml)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new XmlHandler(xmppConn_OnWriteXml), new object[] { this, xml });
                return;
            }
            Console.WriteLine("SEND XML");
            Console.WriteLine(xml);
            Console.WriteLine("\r\n");
        }

        void xmppConn_OnReadXml(object sender, string xml)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new XmlHandler(xmppConn_OnReadXml), new object[] { this, xml });
                return;
            }
            Console.WriteLine("RECV XML");
            Console.WriteLine(xml);
            Console.WriteLine("\r\n");
        }

        void xmppConn_OnWriteSocketData(object sender, byte[] data, int count)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new agsXMPP.net.ClientSocket.OnSocketDataHandler(xmppConn_OnWriteSocketData), new object[] { sender, data, count });
                return;
            }

            try
            {
                Console.WriteLine("SEND: ");
                Console.WriteLine(System.Text.Encoding.Default.GetString(data, 0, count));
                Console.WriteLine("\r\n");
            }
            catch { }
        }

        void xmppConn_OnReadSocketData(object sender, byte[] data, int count)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new agsXMPP.net.ClientSocket.OnSocketDataHandler(xmppConn_OnReadSocketData), new object[] { sender, data, count });
                return;
            }

            try
            {
                Console.WriteLine("RECV: ");
                Console.WriteLine(System.Text.Encoding.Default.GetString(data, 0, count));
                Console.WriteLine("\r\n");
            }
            catch { }
        }

        private void Wait(string statusMessage)
        {
            int i = 0;
            bool _bWait = true;

            while (_bWait)
            {
                i++;
                if (i == 60)
                    _bWait = false;

                System.Threading.Thread.Sleep(500);
            }
        }

        void Conn_OnSocketError(object sender, Exception ex)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new ErrorHandler(Conn_OnSocketError), new object[] { sender, ex });
                return;
            }

            MessageBox.Show("Socket Error\r\n" + ex.Message + "\r\n" + ex.InnerException);   
        }

        void Conn_OnError(object sender, Exception ex)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        void Conn_OnLogin(object sender)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new ObjectHandler(Conn_OnLogin), new object[] { sender });
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
